package tag

import (
	"context"
	"github.com/gin-gonic/gin"
	grpcTag "go-gin-api/app/proto/tag"
	"go-gin-api/app/request"
	"go-gin-api/app/util/response"
	"net/http"
)
// tag 标签
type Tag struct {
	tagClient grpcTag.TagService
}

func NewTag(tagClient grpcTag.TagService) *Tag {
	return &Tag{
		tagClient: tagClient,
	}
}

// 新增 Tag
func (t *Tag) Add(c *gin.Context) {
	var (
		tagRequest grpcTag.TagRequest
		req request.Tag
	)
	// 参数绑定
	if err := c.ShouldBindJSON(&req); err != nil {
		response.ReturnJson(c, http.StatusBadRequest,-1,err.Error(), nil)
		return
	}
	tagRequest.Name = req.Name
	res, err := t.tagClient.Tag(context.Background(), &tagRequest)
	if err != nil {
		response.ReturnJson(c, http.StatusBadRequest,-1, err.Error(), nil)
		return
	}
	if !res.Res {
		response.ReturnJson(c, http.StatusBadRequest,-1, "error", nil)
		return
	}
	response.ReturnJson(c, http.StatusOK,1, "success", nil)
}

// 编辑 Tag
func (t *Tag) Edit(c *gin.Context) {
	var (
		tagUpdateRequest grpcTag.TagUpdateRequest
		req request.TagUpdate
	)
	// 参数绑定
	if err := c.ShouldBindJSON(&req); err != nil {
		response.ReturnJson(c, http.StatusBadRequest,-1,err.Error(), nil)
		return
	}
	tagUpdateRequest.Id = req.Id
	tagUpdateRequest.Name = req.Name
	res, err := t.tagClient.TagUpdate(context.Background(), &tagUpdateRequest)
	if err != nil {
		response.ReturnJson(c, http.StatusBadRequest,-1, err.Error(), nil)
		return
	}
	if !res.Res {
		response.ReturnJson(c, http.StatusBadRequest,-1, "error", nil)
		return
	}
	response.ReturnJson(c, http.StatusOK,1, "success", nil)
}

// 删除 Tag
func (t *Tag) Delete(c *gin.Context) {
	var (
		tagDelRequest grpcTag.TagDelRequest
		req request.TagDel
	)
	// 参数绑定
	if err := c.ShouldBindJSON(&req); err != nil {
		response.ReturnJson(c, http.StatusBadRequest,-1,err.Error(), nil)
		return
	}
	tagDelRequest.Id = req.Id
	res, err := t.tagClient.TagDel(context.Background(), &tagDelRequest)
	if err != nil {
		response.ReturnJson(c, http.StatusBadRequest,-1, err.Error(), nil)
		return
	}
	if !res.Res {
		response.ReturnJson(c, http.StatusBadRequest,-1, "error", nil)
		return
	}
	response.ReturnJson(c, http.StatusOK,1, "success", nil)
}

// 详情 Tag
func (t *Tag) Info(c *gin.Context) {
	var (
		tagInfoRequest grpcTag.TagInfoRequest
		req request.TagInfo
	)
	// 参数绑定
	if err := c.ShouldBindJSON(&req); err != nil {
		response.ReturnJson(c, http.StatusBadRequest,-1,err.Error(), nil)
		return
	}
	tagInfoRequest.Id = req.Id
	res, err := t.tagClient.TagInfo(context.Background(), &tagInfoRequest)
	if err != nil {
		response.ReturnJson(c, http.StatusBadRequest,-1, err.Error(), nil)
		return
	}
	response.ReturnJson(c, http.StatusOK,1, "success", res)
}


// 列表 Tag
func (t *Tag) List(c *gin.Context) {
	var (
		tagListRequest grpcTag.TagListRequest
		req request.TagList
	)
	// 参数绑定
	if err := c.ShouldBindJSON(&req); err != nil {
		response.ReturnJson(c, http.StatusBadRequest,-1,err.Error(), nil)
		return
	}
	tagListRequest.Status = req.Status
	tagListRequest.Limit = req.Limit
	tagListRequest.Page = req.Page
	tagListRequest.Name = req.Name
	tagListRequest.CreatedAt = req.CreatedAt
	res, err := t.tagClient.TagList(context.Background(), &tagListRequest)
	if err != nil {
		response.ReturnJson(c, http.StatusBadRequest,-1, err.Error(), nil)
		return
	}
	response.ReturnJson(c, http.StatusOK,1, "success", res)
}